'''
Created on Dec 19, 2009

@author: anch
'''
import gamephase.AbstractCheckerGameMode as mBase
import gamemodel.state as mState
import gamemodel.player as mPlayer
import ai.computerMento as mAI_M_C
import ai.computerMento as mAI_S
import interface.ConsoleAgent as itfc

class Trainer(mBase.AbstractCheckerGameMode):
	'''
	classdocs
	'''
	STATUS_CFG = 0
	STATUS_TRAIN = 1
	STATUS_CREATE = 2
	STATUS_END = 3

	def __init__(self):
		'''
		Constructor
		'''
		super(Trainer, self).__init__()
		self.state = None
		self.status = NS.STATUS_CREATE
		
	def execute(self):
		if self.status == NS.STATUS_CREATE :
			self.createGame()
			self.status = NS.STATUS_CFG
		elif self.status == NS.STATUS_CFG :
			self.status = NS.STATUS_TRAIN 
		elif self.status == NS.STATUS_TRAIN :
			self.Tick()
		elif self.status == NS.STATUS_END:
			self.end_game()
		pass
	
	def getGameState(self):
		return self.state
	
	def setGameState(self, state):
		self.state = state
		
	def createGame(self):
		player0 = mPlayer.CheckerPlayer("NOD")
		player0.attachAI(mAI_M_C.AIcomputer)
		player1 = mPlayer.CheckerPlayer("GDI")
		player1.attachAI(mAI_S.AIcomputer)
		state = mState.CheckerState.constructNew(player0, player1)
		self.state = state
		pass
	
	def Tick(self):
		itfc.ConsoleDescriptor.drawState(self.state)
		self.annouceLastMove()
		itfc.ConsoleDescriptor.annouceState(self.state)
		
		player = self.state.getCurrentPlayer()
		move = player.getNextMove(self.state)
		
		if move == None:
			#loose this game
			self.stage = NS.STATUS_END
			itfc.ConsoleDescriptor.annouceFail(player)
			return
		
		# proceed state
		self.take_move(move)
		pass

NS = Trainer